//
//  NSString+HECore.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/3.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "NSString+HECore.h"

static const NSString *KEY_REUSELABEL = @"KreuseLabel";

@implementation NSString (HECore)

- (BOOL) isBlankString {
    if (self == nil || self == NULL) {
        return YES;
    }

    if ([self isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;

}

+ (NSString *)moreThanToPlus:(NSInteger)numb max:(NSInteger)maxValue {
    NSString *str = @"";

    if (numb > maxValue) {
        str = [NSString stringWithFormat:@"%ld+", maxValue];
    }else {
        str = [NSString stringWithFormat:@"%ld", numb];
    }
    return str;
}

+ (NSString *)desensitizationWithName:(NSString *)text {
    NSString *rel = [text substringFromIndex: text.length-1];
    return [NSString stringWithFormat:@"**%@",rel];
}

+ (NSString *)desensitizationWithPhone:(NSString *)text {
    NSString *rel = [text substringToIndex: 5];
    return [NSString stringWithFormat:@"%@******",rel];
}

- (NSString *)removeLastStringWith:(NSString *)removeStr {
    if (!(self.length > 0)) {
        return self;
    }
    NSString *lastString = [self substringFromIndex:self.length - 1];

    if ([lastString isEqualToString: removeStr]) {
        NSString *newStr = [self substringToIndex:[self length] - 1];
        return newStr;
    }
    return self;
}


- (UILabel *)reuseLabel{
    UILabel *reuseLabel = objc_getAssociatedObject(self, &KEY_REUSELABEL);
    return reuseLabel;
}

- (void)setReuseLabel:(UILabel *)reuseLabel{
    objc_setAssociatedObject(self, &KEY_REUSELABEL, reuseLabel , OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (CGSize )getSizeWithFont:(UIFont *)font constrainedWidth:(CGFloat)width numberOfLines:(NSInteger)numberOfLines{
    if (width == 0) {
        width = 1000;
    }
    if (self.reuseLabel == nil) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
        self.reuseLabel = label;
    }
    self.reuseLabel.width = width;
    self.reuseLabel.text = self;
    self.reuseLabel.font = font;
    self.reuseLabel.numberOfLines = numberOfLines;
    [self.reuseLabel sizeToFit];
    CGSize size = self.reuseLabel.frame.size;
    return size;
}

- (NSArray *)separateByStr:(NSString *)str {
    NSArray *arr = [self componentsSeparatedByString: str];
    NSMutableArray *strs = @[].mutableCopy;
    for (NSString *string in arr) {
        if (![DYTools isBlankString:string]) {
            [strs addObject:string];
        }
    }
    return strs;
}

@end
